/**
 * YouTube Related Ratings
 * 
 * Display like/dislike bars for all the videos on YouTube
 * 
 * @author Julius Mueller
 */
(function(e) {
	
	var
		$ = null,
		w = window,
		xip = w.xip || (w.xip = {}),
		
		config = {
			isDebug: true
		},
		
		constant = {
			uid: 127419,
			storeId: "xip-yt",
			remote: {
				home: "http://userscripts.org/scripts/show/127419",
				jquery: "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js",
				gdata: "https://gdata.youtube.com/feeds/api/videos"
			}
		},
		
		selector = {
			feedHeadline: "div.feed-item-main h4",
			oldAnchors: "div.feed-page a.title,a.video-list-item-link,a.related-video,div.result-item-main-content h3 a"
		};
	
	
	function debug() {
		if (config.isDebug) console.log.apply(this, arguments);
	}
	
	
	xip.yt = {
			
		init: function() {
			
			debug("youtube related ratings init");
			
			var s = document.createElement("script");
			s.setAttribute("type", "text/javascript");
			s.setAttribute("src", constant.remote.jquery);
			document.getElementsByTagName("head")[0].appendChild(s);
			
			var self = this;
			
			(function t(){
				if (typeof w.jQuery === "function") self.run(w.jQuery);
				else setTimeout(t, 100);
			})();
		},
		
		// old code waiting to be refactored
		addLikes: function(query) {
			
			debug("found", query);
			
			
			query.each(function(i,v){
				var p, id = v.href.replace(/\S*?v=([^&]+)\S*/, "$1");
				$.getJSON(constant.remote.gdata + "?q="+id+"&v=2&alt=jsonc&callback=?", function(e){
					if (typeof e === "object" && e.data.totalItems > 0 && e.data.items[0].rating) {
						p = e.data.items[0].rating * 20;
						$(v).append('<div class="watch-sparkbars"><div style="width: '+p+'%" class="watch-sparkbar-likes"></div><div style="width: '+(100-p)+'%" class="watch-sparkbar-dislikes"></div></div>');
					} else debug("bad response", id, v, e);
					
				});
			});
			
		},
		
		run: function(jQuery) {
			
			var self = this;
			
			this.load();
			
			$ = jQuery;
			$(selector.feedHeadline).css("max-height", "100em");
			
			var page = $("#page");
			
			debug(page);
			
			if (page.length > 0) {
				
				if (page[0].className.trim() === "") {
					this.initPageFeed();
				}
				
				if (page.hasClass("watch")) {
					this.initPageWatch();
				}
				
				if (page.hasClass("search-base")) {
					this.initPageSearch();
				}
				
				if (page.hasClass("channel")) {
					this.initPageChannel();
				}
				
				if (page.hasClass("browse-videos")) {
					this.initPageBrowse();
				}
				
			}
			
			this.addLikes($(selector.oldAnchors));
			
			
			// more related
			$("#watch-more-related-button,button.feed-load-more").click(function(){
				(function t(){
					var e = $("#watch-more-related a.related-video");
					if (e.length === 0) setTimeout(t, 500);
					else self.addLikes(e);
				})();
			});
			
		},
		
		initPageFeed: function() {
			debug("init-page-feed");
		},
		
		initPageWatch: function() {
			debug("init-page-watch");
		},
		
		initPageSearch: function() {
			debug("init-page-search");
		},
		
		initPageChannel: function() {
			debug("init-page-channel");
		},
		
		initPageBrowse: function() {
			debug("init-page-browse");
			this.addLikes($("a.ux-thumb-wrap"));
		},
		
		load: function() {
			try {
				o = JSON.parse(localStorage[constant.storeId]);
				if (typeof o === "object" && o !== null) {
					config = $.extend({}, config, o);
				}
			} catch (ex) {
				debug(ex);
			}
		},
		
		save: function() {
			localStorage[constant.storeId] = JSON.stringify(config);
		}
		
	};
	
	xip.yt.init();
	
})();
